package com.amkai.aes.webservice.binary.objecthandler.billing;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.amkai.aes.aescommon.service.binary.job.billing.InsuranceBillingHistoryJob;
import com.amkai.aes.generatedbeans.bill.BillStatusKind;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class InsuranceBillingHistoryHandler implements JobObjectHandler<InsuranceBillingHistoryJob, Serializable, Serializable> {

	private static final Log logger = LogFactory.getLog(InsuranceBillingHistoryHandler.class);
	protected static final String CHARGE_ID = "chargeId";

	@Override
	public Boolean process(InsuranceBillingHistoryJob job,
			Serializable object) throws Exception {
		try {
			//AES-16992
			//String hqlStr = "select billCharge.id from BillCharge as billCharge where (billCharge.charge.id = :" + CHARGE_ID + " AND billCharge.bill.billed = true AND billCharge.bill.kindWrapper.id = 0)";
			String hqlStr = "select billCharge.id from BillCharge as billCharge where (billCharge.charge.id = :" + CHARGE_ID + " AND billCharge.bill.billStatusWrapper.id IN (:billed, :mark_as_billed, :rebill) AND billCharge.bill.kindWrapper.id = 0)";
			
			SessionManager sm = SiteManager.Start();
			Query q = sm.execute(hqlStr);
			q.setInteger("billed", BillStatusKind.BILLED.ordinal());
			q.setInteger("mark_as_billed", BillStatusKind.MARK_AS_BILLED.ordinal());
			q.setInteger("rebill", BillStatusKind.REBILL.ordinal());
			q.setLong(CHARGE_ID, job.chargeId);
			
			return !q.list().isEmpty();
			
		} catch (Exception e) {
			logger.error("InsuranceBillingHistoryHandler: ", e);
			throw e;
		}
	}

}
